package HP.p0912;

import java.lang.reflect.*;

public class TestFs {
    public static void main(String[] args) throws Exception {
        //new TestFs().t1();
        //new TestFs().t2();
        new TestFs().t5();
    }

    /**
     * 拿到所有的属性
     */
    public void t1() {
        String className = "HP.p0912.Student";
        try {
            Class<?> clz = Class.forName(className);
            Field[] fs = clz.getDeclaredFields();
            for (Field f : fs
                    ) {
                //System.out.println(f.getName());
                System.out.println("f.getName() = " + f.getName());
                System.out.println("f.getType() = " + f.getType());
                System.out.println("f.getModifiers() = " + Modifier.toString(f.getModifiers()));
                System.out.println("-----------------");
            }
            System.out.println("拿到单个属性");
            Field f = clz.getDeclaredField("age");
            System.out.println(f.getName() + "\t" + f.getType().getName() + "\t" + Modifier.toString(f.getModifiers()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    } //t1: 拿到属性

    /**
     * 给属性赋值
     */
    public void t2() {
        String className = "HP.p0912.Student";
        Class<?> clz = null;
        try {
            clz = Class.forName(className);
            Object objectInstance = clz.newInstance();
            Field f = clz.getDeclaredField("money");
            f.setAccessible(true);
            f.set(objectInstance, 12345);

            Field f1 = clz.getDeclaredField("sex");
            f1.setAccessible(true);
            f1.set(objectInstance, true);
            System.out.println("-----------拿到值-----------");
            System.out.println(f.get(objectInstance));
            System.out.println(f1.get(objectInstance));
        } catch (Exception e) {
            e.printStackTrace();
        }
    } // 给属性赋值

    /**
     * 得到并且调用无参构造方法
     */
    public void t3() {
        String className = "HP.p0912.Student";
        try {
            Class clz = Class.forName(className);
            Constructor ct = clz.getDeclaredConstructor(null);
            Object obj = ct.newInstance(null);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    } // 无参构造

    /**
     * 得到并且调用带参构造方法
     */
    public void t4() throws Exception {
        String className = "HP.p0912.Student";
        Class clz = Class.forName(className);
        Constructor ct = clz.getDeclaredConstructor(String.class, boolean.class, int.class, double.class);
        Object ob = ct.newInstance("wy", true, 33, 4545.4);

        System.out.println(((Student) ob).age);
    } //带参构造

    /**
     * 得到所有的方法
     */
    public void t5() {
        String className = "HP.p0912.Student";
        try {
            Class clz = Class.forName(className);
            Method[] ms = clz.getDeclaredMethods();
            for (Method m : ms
                    ) {
                System.out.println("m.getName() = " + m.getName());
                System.out.println("m.getReturnType() = " + m.getReturnType());
                System.out.println("m.getParameterCount() = " + m.getParameterCount());
                System.out.println("Modifier.toString(m.getModifiers()) = " + Modifier.toString(m.getModifiers()));
                //System.out.println("m.getParameterTypes() = " + m.getParameterTypes());
                Class[] classType = m.getParameterTypes();
                for (Class cc : classType
                        ) {
                    System.out.print("cc.getName() = " + cc.getName() + "\t");
                    System.out.println("cc))))))))))))))))))))))))) = " + cc);
                }
                System.out.println("--------------0000--------");

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 得到并调用指定的方法
     */
    public void t6() {

    }
}